home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / CENVIW2.ZIP / KEYPUSH.LIB < prev    next >
Text File  |  1993-09-13  |  9KB  |  264 lines

  1. // KeyPush.lib - Windows routines to control, or mimic, the pushing of
  2. //               keys on the keyboard.  This is one method of controlling
  3. //               windows applications.  The functions in this library work
  4. //               by sending keyboard messages to the focus window as if
  5. //               the key messages were actually being sent by Windows.
  6. //
  7. // The functions in this library are:
  8. //  KeyStroke([Holdkey1,HoldKey2,HoldKey3...]Character)
  9. //  KeyStroke([Holdkey1,HoldKey2,HoldKey3...]VirtualKeyCode)
  10. //  KeyStroke([Holdkey1,HoldKey2,HoldKey3...]KeyString)
  11. //   - Press and release a key or keys.  If HoldKeys are given then will mimic
  12. //     that these keys are pressed before KeyCode or KeyString, and then
  13. //     will press and release KeyCode or KeyString, and then will release
  14. //     HoldKeys in reverse order.
  15. //   Examples: To press the 'S' key: KeyStroke('S');
  16. //     To type a sentence: KeyStroke("On top of Old Smokey!");
  17. //     To type Alt-F1: KeyStroke(VK_ALT,VK_F1);
  18. //     To simulate Ctl-Alt-R: KeyStroke(VK_CONTROL,VK_ALT,'R');
  19. //  VKeyDown(KeyCode)
  20. //   - Press virtual key for Window.
  21. //     Key is not released.
  22. //   Example: KeyDown(VK_SHIFT)
  23. //  VKeyUp(KeyCode)
  24. //   - Release key for focus Window.
  25. //   Example: KeyUp(VK_SHIFT)
  26. //  SpeedKeys(AsciiString)
  27. //     Speed keys sends only WM_CHAR messages to an application and is much
  28. //     faster thatn KeyStrok(), but should only be used for regular ascii
  29. //     strings passed to applications that don't need to for each specific
  30. //     keystroke.  So, if KeyStroke() is too slow and you're entering a
  31. //     stretch of plain text, then use this function.
  32.  
  33.  
  34. #define VK_LBUTTON          0x01
  35. #define VK_RBUTTON          0x02
  36. #define VK_CANCEL           0x03
  37. #define VK_MBUTTON          0x04
  38. #define VK_BACK             0x08
  39. #define VK_TAB              0x09
  40. #define VK_CLEAR            0x0C
  41. #define VK_RETURN           0x0D
  42. #define VK_SHIFT            0x10
  43. #define VK_CONTROL          0x11
  44. #define VK_MENU             0x12
  45. #define VK_ALT              VK_MENU
  46. #define VK_PAUSE            0x13
  47. #define VK_CAPITAL          0x14
  48. #define VK_ESCAPE           0x1B
  49. #define VK_SPACE            0x20
  50. #define VK_PRIOR            0x21
  51. #define VK_NEXT             0x22
  52. #define VK_END              0x23
  53. #define VK_HOME             0x24
  54. #define VK_LEFT             0x25
  55. #define VK_UP               0x26
  56. #define VK_RIGHT            0x27
  57. #define VK_DOWN             0x28
  58. #define VK_SELECT           0x29
  59. #define VK_PRINT            0x2A
  60. #define VK_EXECUTE          0x2B
  61. #define VK_SNAPSHOT         0x2C
  62. #define VK_INSERT           0x2D
  63. #define VK_DELETE           0x2E
  64. #define VK_HELP             0x2F
  65. /* VK_A thru VK_Z are the same as their ASCII equivalents: 'A' thru 'Z' */
  66. /* VK_0 thru VK_9 are the same as their ASCII equivalents: '0' thru '9' */
  67. #define VK_NUMPAD0          0x60
  68. #define VK_NUMPAD1          0x61
  69. #define VK_NUMPAD2          0x62
  70. #define VK_NUMPAD3          0x63
  71. #define VK_NUMPAD4          0x64
  72. #define VK_NUMPAD5          0x65
  73. #define VK_NUMPAD6          0x66
  74. #define VK_NUMPAD7          0x67
  75. #define VK_NUMPAD8          0x68
  76. #define VK_NUMPAD9          0x69
  77. #define VK_MULTIPLY         0x6A
  78. #define VK_ADD              0x6B
  79. #define VK_SEPARATOR        0x6C
  80. #define VK_SUBTRACT         0x6D
  81. #define VK_DECIMAL          0x6E
  82. #define VK_DIVIDE           0x6F
  83. #define VK_F1               0x70
  84. #define VK_F2               0x71
  85. #define VK_F3               0x72
  86. #define VK_F4               0x73
  87. #define VK_F5               0x74
  88. #define VK_F6               0x75
  89. #define VK_F7               0x76
  90. #define VK_F8               0x77
  91. #define VK_F9               0x78
  92. #define VK_F10              0x79
  93. #define VK_F11              0x7A
  94. #define VK_F12              0x7B
  95. #define VK_F13              0x7C
  96. #define VK_F14              0x7D
  97. #define VK_F15              0x7E
  98. #define VK_F16              0x7F
  99. #define VK_NUMLOCK          0x90
  100.  
  101.  
  102. KeyStroke(Key1,Key2,Key3/*etc...*/)
  103. {
  104.    _argCount = va_arg();
  105.    // if more than one parameter, then hold down all but the last one
  106.    for ( k = 1; k < _argCount; k++ )
  107.       VKeyDown(va_arg(k-1));
  108.  
  109.    _key = va_arg(_argCount-1);
  110.    // Treat differently depending on whether Virtual Key Code, char, or string
  111.    if ( CMM_BYTE == DataType(_key) ) {
  112.       if ( 1 == DataDimension(_key) ) {
  113.          // send entire string one character at a time
  114.          for ( k = 0; _key[k]; k++ )
  115.             CharacterStroke(_key[k]);
  116.       } else {
  117.          // send a single character out the port
  118.          CharacterStroke(_key);
  119.       }
  120.    } else {
  121.       // simply a virtual key code
  122.       VKeyDown(_key);
  123.       VKeyUp(_key);
  124.    }
  125.  
  126.    // if more than one parameter, then reverse release all but the last one
  127.    for ( k = 1; k < _argCount; k++ )
  128.       VKeyUp(va_arg(_argCount-k-1));
  129. }
  130.  
  131. VKeyDown(KeyCode)
  132. {
  133.    // Save the current state of the 256-byte keyboard buffer
  134.    _keyBuffer = GetKeyboardState();
  135.  
  136.    // build hi word of lParam for the WM_KEYDOWN or WM_SYSKEYDOWN message
  137.    // 1: Get Scan Code for this virtual key
  138.    _HiWord = MapVirtualKey(KeyCode,0);
  139.    // 2: Set bit for if this is an extended key
  140.    if ( (VK_PRIOR <= KeyCode && KeyCode <= VK_HELP)
  141.      || (VK_F1 <= KeyCode && KeyCode <= VK_F16) )
  142.       _HiWord |= 0x100;
  143.    // 3: If ALT key is down but not the control key, then set that bit
  144.    //    and change message to WM_SYSKEYDOWN
  145.    if ( (KeyCode == VK_ALT  ||  (GetKeyState(VK_ALT) & 0x8000) )
  146.      && !(GetKeyState(VK_CONTROL) & 0x8000) )
  147.       _HiWord |= 0x2000, _message = WM_SYSKEYDOWN;
  148.    else
  149.       _message = WM_KEYDOWN;
  150.  
  151.    // save in _keyBuffer that this key is now down
  152.    _keyBuffer[KeyCode] = (_KeyBuffer[KeyCode] ^ 0x01) | 0x80;
  153.    SetKeyboardState(_keyBuffer);
  154.  
  155.    // send message to window that this key is being pressed
  156.    PostMessage(GetFocus(),_message,KeyCode,1,_HiWord);
  157. }
  158.  
  159.  
  160. VKeyUp(KeyCode)
  161. {
  162.    // Save the current state of the 256-byte keyboard buffer
  163.    _keyBuffer = GetKeyboardState();
  164.  
  165.    // build hi word of lParam for the WM_KEYUP or WM_SYSKEYUP message
  166.    // 1: Get Scan Code for this virtual key
  167.    _HiWord = MapVirtualKey(KeyCode,0);
  168.    // 2: Set bit for if this is an extended key
  169.    if ( (VK_PRIOR <= KeyCode && KeyCode <= VK_HELP)
  170.      || (VK_F1 <= KeyCode && KeyCode <= VK_F16) )
  171.       _HiWord |= 0x100;
  172.    // 3: If ALT key is down but not the control key, then set that bit
  173.    //    and change message to WM_SYSKEYDOWN
  174.    if ( (KeyCode == VK_ALT  ||  (GetKeyState(VK_ALT) & 0x8000) )
  175.      && !(GetKeyState(VK_CONTROL) & 0x8000) )
  176.       _HiWord |= 0x2000, _message = WM_SYSKEYUP;
  177.    else
  178.       _message = WM_KEYUP;
  179.    // 4: set hi 2 bits that key WAS down
  180.    _HiWord |= 0xC000;
  181.  
  182.    // save in _keyBuffer that this key is now up
  183.    _keyBuffer[KeyCode] &= 0x7F;
  184.    SetKeyboardState(_keyBuffer);
  185.  
  186.    // send message to window that this key is being pressed
  187.    PostMessage(GetFocus(),_message,KeyCode,1,_HiWord);
  188. }
  189.  
  190. SpeedKeys(AsciiString)
  191. {
  192.    _focus = GetFocus();
  193.    for ( c = AsciiString; c[0]; c++ )
  194.       PostMessage(_focus,WM_CHAR,c[0],1,0);
  195. }
  196.  
  197.  
  198. /***********************************************************
  199.  *** PRIVATE UTILITIES USED BY THE ABOVE PUBLIC ROUTINES ***
  200.  ***********************************************************/
  201.  
  202. CharacterStroke(c) // press this character.  This is only tricky in that
  203. {                  // must check SHIFT state for alphabetic characters
  204.    // Get virtual codes for this character
  205.    _vkeystate = DynamicLink("KEYBOARD","VKKEYSCAN",SWORD16,PASCAL,c)
  206.    if ( -1 != _vkeystate ) {
  207.       vkey = _vkeystate & 0xFF;
  208.       switch( _vkeystate >> 8 ) {
  209.          case 0:  // no shift state
  210.          case 1:  // shift state
  211.             if ( (0 != (GetKeyState(VK_SHIFT) & 0x80)) ^
  212.                  (0 != (GetKeyState(VK_CAPITAL) & 0x01)) ^
  213.                  (0 != (_vkeystate & 0x100)) ) {
  214.                // need to toggle the shift key to change case of this character
  215.                if ( GetKeyState(VK_SHIFT) & 0x80 )
  216.                   VKeyUp(VK_SHIFT), KeyStroke(vkey), VKeyDown(VK_SHIFT